<?php 

namespace Weixin\Model;

use Common\Model\RelationModel;

class SuipaiModel extends RelationModel{
    
    protected $_auto	 =	 array(
        array('iftj', 0, self::MODEL_INSERT,'string'),
        array('ifindex', 0, self::MODEL_INSERT,'string'),
        array('status', 0, self::MODEL_INSERT,'string'),
        array('sort', '0', self::MODEL_INSERT,'string'),
        array('create_time','time',self::MODEL_INSERT,'function')
    );
    
    //随拍作品添加模型
    public function suipaiAddModel($data){
        if (empty($data)) {
            $this->error = '添加数据不能为空！';
            return false;
        }
        
        $data['status']=false;
        $data['create_time']=time();
        $model=M('Suipai');
        $result = $model->create($data, 1);
        if ($result) {
            $workid = $model->add($result);
            if ($workid) {
                return $workid;
            } else {
                $this->error = '作品添加失败';
                return false;
            }
        } else {
            return false;
        }
    }
    
    //构建随手拍列表数据
    public function getSpList2($userid, $firstRow,$listRows){
    
    
        $list = $this
        ->table(array(
            C('DB_PREFIX').'suipai'=>'S',
            C('DB_PREFIX').'user'=>'U',
        )
        )
        ->where('S.userid=U.userid and S.status=true')
        ->field('S.title,S.content,S.id,S.create_time')
        ->order('S.create_time DESC')
        ->limit($firstRow . ',' . $listRows)
        ->select();
    
    
        foreach($list as $key=>$value){
            $list[$key]['create_time']=format_time($value['create_time'], 'Y-m-d H:i');
            $list[$key]['title'] = hy_substr($value['title'], 12);
        }
    
        return $list;
    }
    
    //构建随手拍列表数据
    public function getSpList($userid, $firstRow,$listRows){
        
        
        $list = $this
        ->table(array(
            C('DB_PREFIX').'suipai'=>'S',
            C('DB_PREFIX').'user'=>'U',
        )
        )
        ->where('S.userid=U.userid and S.status=true')
        ->field('S.title,S.msgnum,S.content,S.id,S.create_time,S.image,S.all_pic,S.goodnum,U.nickname,U.uavatar,U.openuid,U.userid,U.ifrz')
        ->order('S.create_time DESC')
        ->limit($firstRow . ',' . $listRows)
        ->select();
        
        
        foreach($list as $key=>$value){
            $list[$key]['create_time']=format_time($value['create_time'], 'Y-m-d H:i');
            $list[$key]['uavatar']=getUavatar($value['uavatar']);
            $list[$key]['image']=WEB_URL.$value['image'];
            $list[$key]['title'] = hy_substr($value['title'], 12);
            $yf_thumb = array();
            $yf_pic = array();
            
            if(!empty($value['all_pic']))
            {
                $yf_arr = json_decode($value['all_pic'], true);
                $yf_temp = array();
                foreach($yf_arr as $k => $v)
                {
                    //$yf_thumb[] = WEB_URL.$v;
                    $yf_ex = explode("/", $v);
                    $yf_ex[count($yf_ex)-1] = str_replace("yf_", "", $yf_ex[count($yf_ex)-1]);
                    $yf_pic[] = $temp = WEB_URL.implode("/", $yf_ex);
                    $yf_thumb[] = array(
                        'thumb' => $v,
                        'pic' => $temp
                    );
                }
            }
            else
            {
                $yf_arr = array();
                //$yf_thumb[] = $yf_pic[] = WEB_URL.$value['image'];
                $yf_thumb[] = array(
                    'thumb' => $value['image'],
                    'pic' => WEB_URL.$value['image']
                );
                $yf_pic[] = WEB_URL.$value['image'];
            }
             
            $list[$key]['thumb']=$yf_thumb;
             
            $list[$key]['pic'] = implode(',', $yf_pic);
            $list[$key]['fans'] = M('UserFans')->where(array('myid' => $userid, 'userid' => $value['userid']))->getField('status');
        }
    
        return $list;
    }
    
}